package com.jk.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.dao.*;
import com.jk.pojo.*;
import com.jk.utils.ReturnMsgUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author jisiqi
 * @version 1.0
 * @description: TODO
 * @date 2022/10/19 9:49
 */
@Service
public class SystemService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private InformationDao informationDao;

    @Autowired
    private CarouselDao carouselDao;

    @Autowired
    private ProclamationDao proclamationDao;

    @Autowired
    private LogDao logDao;

    @Autowired
    private  RoleDao  roleDao;

    public Map getLabelList(LabelEntity labelEntity) {
        try {
            QueryWrapper<LabelEntity> Wrapper = new QueryWrapper<LabelEntity>();
            if (labelEntity.getLabelName()!=null && labelEntity.getLabelName()!=""){
                Wrapper.eq("label_name",labelEntity.getLabelName());
            }
            List<LabelEntity> list = labelDao.selectList(Wrapper);
            System.out.println(list);
            return ReturnMsgUtils.returnMsg(200,"标签管理表数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public boolean updateState(Integer id, Integer state) {
        try {
            LabelEntity labelEntity = new LabelEntity();
            labelEntity.setLabelId(id);
            labelEntity.setUseState(state);
            labelDao.updateById(labelEntity);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteLabelId(Integer id) {
        try {
            labelDao.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public Map getInformationList(InformationEntity informationEntity) {
        try {
            String minDate = informationEntity.getMinDate();
            String maxDate = informationEntity.getMaxDate();
            QueryWrapper<InformationEntity>  Wrapper = new QueryWrapper<>();
            if (informationEntity.getInformationTitle()!=null && informationEntity.getInformationTitle()!=""){
                Wrapper.like("information_title",informationEntity.getInformationTitle());
            }
            if (StringUtils.isNotEmpty(informationEntity.getMinDate())){
                Wrapper.ge("operation_date",minDate);
            }
            if (StringUtils.isNotEmpty(maxDate)){
                Wrapper.le("operation_date",maxDate);
            }
            List<InformationEntity> list = informationDao.selectList(Wrapper);
            return ReturnMsgUtils.returnMsg(200,"标签管理表数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public Map getCarouselList(CarouselEntity carouselEntity) {
        try {
            String minDate = carouselEntity.getMinDate();
            String maxDate = carouselEntity.getMaxDate();
            String carouselImgName = carouselEntity.getCarouselImgName();
            QueryWrapper<CarouselEntity> Wrapper = new QueryWrapper<>();
            //名字非空
            if (StringUtils.isNotEmpty(carouselImgName)){
                Wrapper.like("carousel_img_name",carouselImgName);
            }
            //开始时间判断非空
            if (StringUtils.isNotEmpty(minDate)){
                Wrapper.ge("publish_date",minDate);
            }
            //结束时间判断非空
            if (StringUtils.isNotEmpty(maxDate)){
                Wrapper.le("publish_date",maxDate);
            }
            List<CarouselEntity> list = carouselDao.selectList(Wrapper);
            return ReturnMsgUtils.returnMsg(200,"轮播图表数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public Map getSystemList(ProclamationEntity proclamationEntity) {
        try {
            QueryWrapper<ProclamationEntity> Wrapper = new QueryWrapper<ProclamationEntity>();
            //消息内容非空
            if (StringUtils.isNotEmpty(proclamationEntity.getInformationContent())){
                Wrapper.like("information_content",proclamationEntity.getInformationContent());
            }
            //开始时间非空
            if (StringUtils.isNotEmpty(proclamationEntity.getMinDate())){
                Wrapper.ge("operation_date",proclamationEntity.getMinDate());
            }
            //结束时间非空
            if (StringUtils.isNotEmpty(proclamationEntity.getMaxDate())){
                Wrapper.le("operation_date",proclamationEntity.getMaxDate());
            }
            List<ProclamationEntity> list = proclamationDao.selectList(Wrapper);
            return ReturnMsgUtils.returnMsg(200,"系统公告表数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public Map getLogList(LogEntity logEntity) {
        try {
            QueryWrapper<LogEntity> Wrapper = new QueryWrapper<LogEntity>();
            //消息内容非空
            if (StringUtils.isNotEmpty(logEntity.getOperationMan())){
                Wrapper.like("operation_man",logEntity.getOperationMan());
            }
            //开始时间非空
            if (StringUtils.isNotEmpty(logEntity.getMinDate())){
                Wrapper.ge("operation_date",logEntity.getMinDate());
            }
            //结束时间非空
            if (StringUtils.isNotEmpty(logEntity.getMaxDate())){
                Wrapper.le("operation_date",logEntity.getMaxDate());
            }
            List<LogEntity> list = logDao.selectList(Wrapper);
            return ReturnMsgUtils.returnMsg(200,"系统公告表数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public boolean deleteInformationId(Integer id) {
        try {
            informationDao.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteCarouselId(Integer id) {
        try {
            carouselDao.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteSystemId(Integer id) {
        try {
            proclamationDao.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public Map getRoleList() {
        try {
            Wrapper<RoleEntity> Wrapper = new QueryWrapper<>();
            List<RoleEntity> list = roleDao.selectList(Wrapper);
            return ReturnMsgUtils.returnMsg(200,"角色管理表数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public boolean deleteRoleId(Integer id) {
        try {
            roleDao.deleteById(id);
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    public Map addRole(RoleEntity roleEntity) {
        try {
            int insert = roleDao.insert(roleEntity);
            return ReturnMsgUtils.returnMsg(200,"新增成功",insert);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"新增失败",null);
        }
    }

    /**
     * @Author: binzi
     * @Description:新增公告
     * @Date: 2022/10/24 10:19
     * @Return: java.util.Map
     **/
    public Map addInform(ProclamationEntity proclamationEntity) {
        try {
            proclamationEntity.setPublisher("李明明");
            proclamationEntity.setState(1);
            int insert = proclamationDao.insert(proclamationEntity);
            return ReturnMsgUtils.returnMsg(200,"新增成功",proclamationEntity);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"新增失败",null);
        }
    }
}
